package com.gewu.pm.ui.activity.clockin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.TestBean;
import com.gewu.pm.ui.activity.filter.FilterMyApplicationActivity;
import com.gewu.pm.ui.adapter.MakeUpApplicationAdapter;
import com.gewu.pm.ui.popup.SelectTimePopup;
import com.hjq.bar.TitleBar;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的补卡申请列表页面
 */
public final class MyMakeUpApplyActivity extends AppActivity
        implements BaseAdapter.OnItemClickListener {

    private TitleBar mTitleBar;
    private TextView tvFilter;
    private RecyclerView rvList;

    private MakeUpApplicationAdapter mAdapter;
    private final List<TestBean> mDataList = new ArrayList<>();

    private String mCurrentFilter = "全部";  // 当前筛选类型

    public static void start(Context context) {
        Intent intent = new Intent(context, MyMakeUpApplyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.my_make_up_apply_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.tb_title);
        tvFilter = findViewById(R.id.tv_filter);
        rvList = findViewById(R.id.rv_list);

        // 设置RecyclerView
        rvList.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MakeUpApplicationAdapter(this);
        // 必须在setAdapter之前设置监听器
        mAdapter.setOnItemClickListener(this);
        rvList.setAdapter(mAdapter);

        // 设置点击事件
        setOnClickListener(tvFilter);
    }

    @Override
    protected void initData() {
        // 加载模拟数据
        loadData(null);
    }

    /**
     * 加载数据
     * @param type 筛选类型：null=全部，1=审批中，2=已通过，3=已驳回，4=已撤销
     */
    private void loadData(String type) {
        mDataList.clear();

        // 模拟数据
        if (type == null || type.equals("1")) {
            mDataList.add(new TestBean("1", "补卡1", "1"));  // 审批中
        }
        if (type == null || type.equals("2")) {
            mDataList.add(new TestBean("2", "补卡2", "2"));  // 已通过
        }
        if (type == null || type.equals("3")) {
            mDataList.add(new TestBean("3", "补卡3", "3"));  // 已驳回
        }
        if (type == null || type.equals("4")) {
            mDataList.add(new TestBean("4", "补卡4", "4"));  // 已撤销
        }

        mAdapter.setData(mDataList);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_filter) {
            // 显示筛选菜单
            showFilterMenu();
        }
    }

    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        FilterMyApplicationActivity.start(getActivity());
    }

    /**
     * 显示筛选菜单
     */
    private void showFilterMenu() {
        List<String> menuList = new ArrayList<>();
        menuList.add("全部");
        menuList.add("审批中");
        menuList.add("已通过");
        menuList.add("已驳回");
        menuList.add("已撤销");

        new SelectTimePopup.Builder(this)
                .setList(menuList)
                .setListener((position, item) -> {
                    mCurrentFilter = item;
                    tvFilter.setText(item);

                    // 根据选择筛选数据
                    String filterType = null;
                    switch (item) {
                        case "审批中":
                            filterType = "1";
                            break;
                        case "已通过":
                            filterType = "2";
                            break;
                        case "已驳回":
                            filterType = "3";
                            break;
                        case "已撤销":
                            filterType = "4";
                            break;
                    }
                    loadData(filterType);
                })
                .showAsDropDown(tvFilter);
    }

    /**
     * 列表Item点击事件
     */
    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        TestBean item = mAdapter.getItem(position);
        // 跳转到补卡详情页面，传递id和type
        MyMakeUpApplyDetailActivity.start(this, item.getId(), item.getType());
    }
}
